package com.m4f.city.business.oauth2.impl;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

public class M4FEntryPoint implements AuthenticationEntryPoint {

	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException auth) throws IOException, ServletException {
		
		/*
		 * If there isn't a client_id parameter or the requested uri is not /oauth/confirm_access, then is a normal login.
		 */
		if(request.getParameter("client_id") != null && !("").equals(request.getParameter("client_id"))) {
			response.sendRedirect(request.getContextPath()+"/login?client_id="+request.getParameter("client_id"));
		} else {
			response.sendRedirect(request.getContextPath()+"/login");
		}
		
	}
}
